package org.example.cicdi.service.impl.ext;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import org.example.cicdi.enums.EnumWhiteColumnAnnotation;
import org.example.codegenerate.modelext.WebVo.domian.column.ColumnVoDomain;
import org.example.codegenerate.modelext.WebVo.domian.column.DbVo;
import org.example.codegenerate.modelext.WebVo.domian.table.WebModelVo;
import org.springframework.stereotype.Service;

import javax.persistence.Column;
import java.lang.reflect.Field;

@Service
public class DbVoAnnotatonImpleServiceExt extends AbstactFieldAnnotationServiceExt {

	@Override
	public String getCode() {
		return EnumWhiteColumnAnnotation.DbVo.getCode();
	}

	@Override
	public void execute(ColumnVoDomain columnVoDomain, Field field, WebModelVo model) {

		DbVo db = columnVoDomain.getDbVo();
		/*if (db == null) {
			db = new org.jeecg.modules.codegenerate.modelext.WebVo.domian.column.DbVo();
			columnVoDomian.setDbVo(db);
		}*/

		if (field.getAnnotation(Column.class) != null) {
			Column vo = field.getAnnotation(Column.class);
			db.setDbDefaultVal(null); //如果这里要实现的话，需要单独开个方法：jpa对象->class.newInstance()->反射获取字段值
			db.setDbFieldName(vo.name());
			
			String comment = vo.columnDefinition().toLowerCase();
			if (StringUtils.isNotBlank(comment)) {
				db.setDbFieldTxt(comment.substring(comment.indexOf("'") + 1, comment.length() - 1));
			}
			
			db.setDbIsKey(vo.unique() ? 1 : 0);
			db.setDbIsNull(vo.nullable() ? 1 : 0);
			db.setDbLength(vo.length());
			db.setDbPointLength(vo.precision());
			db.setDbType(field.getType().getSimpleName());
			db.setDbPointLength(vo.scale());
		} /*else {
			JoinColumn vo = field.getAnnotation(JoinColumn.class);
			if (vo != null) {
				db.setDbDefaultVal(null); //如果这里要实现的话，需要单独开个方法：jpa对象->class.newInstance()->反射获取字段值
				db.setDbFieldName(vo.name());
				String comment = vo.columnDefinition().toLowerCase();
				if (StringUtils.isNotBlank(comment)) {
					db.setDbFieldTxt(comment.substring(comment.indexOf("comment") + 1));
				}
				db.setDbIsKey(vo.unique() ? 0 : 1);
				db.setDbIsNull(vo.nullable() ? 0 : 1);
				db.setDbLength(null);
				db.setDbPointLength(null);
				db.setDbType(field.getType().getSimpleName());
				db.setDbPointLength(null);
			}
		}
		
		return column;*/
	}
}
